﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class CreatNvPath : MonoBehaviour
{
    public UnityUWBClient client;

    public Vector3 PlayLocal;
    public Vector3 TagLocal;

    private NavMeshAgent navMeshAgent;
    private NavMeshPath path;
    public LineRenderer lineRenderer;

    public float r = 2;

    public bool isOK = false;

    // Start is called before the first frame update
    void Start()
    {
        navMeshAgent = GetComponent<NavMeshAgent>();
        path = new NavMeshPath();
    }

    float sumTime = 0;
    // Update is called once per frame
    void Update()
    {
        if (client.isLogin)
        {
            PlayLocal = transform.position;
            if ((PlayLocal- TagLocal).magnitude<r)
            {
                isOK = true;
                return;
            }
            else
            {
                isOK = false;
            }
            sumTime += Time.deltaTime;
            if (sumTime > 0.3)
            {
                sumTime = 0;
                navMeshAgent.CalculatePath(TagLocal, path);
                if (path.corners.Length < 2)
                {
                    return;
                }
                else
                {
                    lineRenderer.positionCount = path.corners.Length;
                    Vector3[] tmpCor = path.corners;
                    for (int i = 0; i < tmpCor.Length; i++)
                    {
                        tmpCor[i].y += 1f;
                    }

                    lineRenderer.SetPositions(tmpCor);
                }
            }
        }
    }
}
